<?php
/*
 * Created on 11/03/2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class LoginsController extends AppController{
 	var $name = 'Logins';
 	var $helpers = array('Form', 'Html', 'Javascript', 'Time','Session');
 	var $uses=array('User','NewPasswordRequest');
 	var $components = array('Cookie','Session');
 	var $user;
 	//public $uses = array('logins');
 	function setUser($user){
 		$this->user = $this->User->find('first',array('conditions'=>array('login'=>$user)));
 	}
 	function logar(){
 		$usuario = $this->data['login']['login'];
 		$senha = $this->data['login']['password'];
 		$this->setUser($usuario);
 		if(!empty($this->user) && $this->user['User']['password'] == md5($senha)){
 			$this->Session->write('User',$this->user['User']);
 			return true; 
 		}else
 			return false;
 	}
 	function logins(){
 		if(!empty($this->data)){
 		 	if($this->logar()){
	 	 		$this->redirect('/main/index');
	 	 	}else{
	 	 		$this->data['erro'] = 1;
	 	 	} 			
 		}

 		$this->render('index');
 	}

 	function logOut(){
 		$this->Session->write('User','');
 		$this->redirect('/logins/logins');
 	}
 	function request_password(){
 		//error_reporting(E_ALL);
		//ini_set('display_errors','On');
 		if($this->data['login']['login'] == ''){
 			$this->data['login_vazio'] = 1;
 			$this->render('index');
 		}else{
 			$this->setUser($this->data['login']['login']);
 			if(empty($this->user)){
 				$this->data['usuario_inexistente'] = 1;
 				$this->render('index');
 			}else{
 				$this->data = $this->user;
 				$this->user['User']['request_new_password'] = date('Y-m-d G:i:s');
 				$this->user['User']['rand_new_password'] = uniqid();
 				$this->User->save($this->user);
 				$this->configuraEmail();
 				$this->Email->to = $this->user['User']['email'];
 				$this->data = $this->user;
 				$this->Email->template = 'password_request';
 				$this->Email->subject = 'Change password information';
 				$this->data['User']['link'] = "http://" . $_SERVER['HTTP_HOST'] . "/logins/new_password_set_form/" . $this->user['User']['rand_new_password'];
 				$this->data['User']['application'] = 'New app';
 				$this->Email->send();
 				$this->render('mail_sent_info_request_password');	
 			}
 		}
 	}
 	function new_password_set_form($uid){
 		$user = $this->NewPasswordRequest->find('first',array('conditions'=>array('rand_new_password'=>$uid)));
 		$this->data['newpassword']['login'] = $user['NewPasswordRequest']['login'];
 		if(empty($user)){
 			
 			$this->render('no_password_request');
 		}
 	}
 	function set_new_password(){
 		//print_r($this->data);
 		if(empty($this->data)){
 			$this->verificaLogado();
 		}else{
 			if($this->data['newpassword']['new_password'] != $this->data['newpassword']['new_password_again']){
 				$this->data['password_diferente'] = 1;
 				$this->render('new_password_set_form');
 			}else if($this->data['newpassword']['new_password'] == ''){
 				$this->data['password_vazio'] = 1;
 				$this->render('new_password_set_form');
 			}else{
 				try{
 					$user = $this->User->find('first',array('conditions'=>array('login'=>$this->data['newpassword']['login'])));
 					$user['User']['password'] = md5($this->data['newpassword']['new_password']);
 					$user['User']['rand_new_password'] = '';
 					$user['User']['request_new_password'] = '';
 					$this->User->save($user);
 					$this->configuraEmail();
 					$this->Email->to = $user['User']['email'];
 					$this->Email->template = 'password_modified';
 					$this->Email->subject = 'Password Modified';
 					$this->data['User']['name'] = $user['User']['name'];
 					$this->Email->send(); 
 				}catch (Exception $e){
 					$this->data['exception'] = $e;
 					$this->render('new_password_set_form');
 				}
 				
 			}
 		}
 	}
 	function login(){
 		$this->inicializaLocale();
 		$this->set('act',$this->action);
 		if (!empty($this->data)){
 			$this->Login->set($this->data);
 			if ($this->Login->validates()) {
 				if ($this->Login->changepassword == '1'){
 					$this->redirect('/change_passwords/edit/' . $this->Login->id);	
 				}else{
 					$this->Session->write('User.name',$this->Login->user_name);
 					$this->Session->write('User.id',$this->Login->id);
 					$this->Session->write('User.last_access_date',$this->Login->last_access_date);
 					$this->User->id = $this->Login->id;
 					$this->User->save(array('last_access_date'=>date('Y-m-d H:i:s')));
 					$this->redirect('/main_menus');	
 				}
			}
 		}else{
 			$this->data = $this->Login->read();
 		}
 	}
 }
?>
